"""
@author: tian-yan-tian-ge
@file: 三创官网.py
@time: 2023/12/6 10:00
"""
from sanchuang import download, download_img
from bs4 import BeautifulSoup
import os
import logging

# 需要的数据的url
for i in range(1):
    path = "../下载三创官网图片/sanchuang_pic"
    # 如果目录不存在，则自动创建目录
    if not os.path.isdir(path):
        os.mkdir(path)

    url = f"https://www.sanchuangedu.cn/"
    text = download(url)
    # print(text)
    # 提取需要的数据
    soup = BeautifulSoup(text, "lxml")
    # 总共25个
    all_book = soup.find_all("table")
    for book in all_book:
        book_cover = book.find("img")["src"]
        # print(book_cover)
        # 获取书名
        # 如果a内部有span标签，string取出的值变成了None
        # book_name_span = book.find_all("a")[1].string
        # 可以用get_text()取值
        book_name = book.find_all("a")[1].get_text()
        book_name = "".join(book_name.strip().split())
        # print(book_name)
        logging.debug(book_name)

        # 下载书封面图片，保存到本地
        filename = f"{path}/{book_name.replace(':','：')}.jpg"
        with open(filename, "wb") as f:
            # 访问封面图片
            f.write(download_img(book_cover))